﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HYWJ
{
    class speech
    {
        private System.Speech.Synthesis.SpeechSynthesizer sp = new System.Speech.Synthesis.SpeechSynthesizer();
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private List<string> message = new List<string>();
        public speech( )
        {
            this.sp.SelectVoice("VW Wang");
            this.sp.Rate = 3;

            this.timer.Interval = 1000;
            this.timer.Tick += new EventHandler(this.timer_Tick);
            this.timer.Start();
            
            this.sp.SpeakCompleted += new EventHandler<System.Speech.Synthesis.SpeakCompletedEventArgs>(this.sp_SpeakCompleted);
        }

        void sp_SpeakCompleted(object sender, System.Speech.Synthesis.SpeakCompletedEventArgs e)
        {
            this.sp.Dispose();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (this.message.Count > 0)
            {
                this.sp.SpeakAsyncCancelAll();
                this.sp.SpeakAsync(this.message[0]);
                this.message.RemoveAt(0);
            }
        }

        internal void Speak(string messageString)
        {
            this.message.Add(messageString);
        }
    }
}
